package cn.anlab.anappframe.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	
	//数据库名称
	private static final String DB_NAME = "anappframe.db";

	public DBHelper(Context context, int version) {
		super(context, DB_NAME, null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		executeSql(db, 0, 1);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		executeSql(db, oldVersion, newVersion);
	}
	
	
	/**
	 * 执行新版本与本地版本差余的SQL
	 * @param db
	 * @param oldVersion 上次安装时的版本
	 * @param newVersion 此次安装时的版本
	 */
	private void executeSql(SQLiteDatabase db, int oldVersion, int newVersion) {
		while((++oldVersion) <= newVersion) {
			executeSqlByVersion(db, oldVersion);
		}
	}
	
	/**
	 * 执行某个版本的SQL
	 * @param version
	 */
	private void executeSqlByVersion(SQLiteDatabase db, int version) {
		switch (version) {
		case 1:			//第1个版本要执行的SQL
		{
			db.execSQL("create table if not exists tb_demo1(uid integer primary key autoincrement, uname text )");
			db.execSQL("create table if not exists tb_demo2(uid integer primary key autoincrement, uname text )");
		}
			break;
		case 2:			//第2个版本要执行的SQL
		{
			db.execSQL("create table if not exists tb_demo3(uid integer primary key autoincrement, uname text )");
		}
			break;
		//~~扩展~~，以后每发一次版本，将该版本要执行的SQL在这里写
		default:
			break;
		}
	}

}
